{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0d85e1e5",
   "metadata": {},
   "source": [
    "* 本周主要内容：AI-NLP:自然语言处理（Natural Language Processing)）\n",
    "* 23春_API_人工智能与机器学习_week09\n",
    "* 记录者：赖文佩"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f31e3632",
   "metadata": {},
   "source": [
    "#  本周主要内容"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "37e373b5",
   "metadata": {},
   "source": [
    "## 自然语言处理\n",
    "> 什么是自然语言   \n",
    ">> 1.语言的本身           \n",
    ">> 2.语言的隐喻        "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9a70ed11",
   "metadata": {},
   "source": [
    "> 什么是自然语言处理   \n",
    ">> 1.Machin Translation（机器翻译）：谷歌、有道翻译        \n",
    ">> 2.Chinese word segmentation（中文自动分词）：锤子手机大爆炸       \n",
    ">> 3.Question Answering（问答系统）：openai、文言一心       \n",
    ">> 4.Information Extracting（信息抽取）        \n",
    ">> 5.Reading comprehension（阅读理解）      \n",
    ">> 6.Automatic summarization(自动摘要)       \n",
    ">> 7.Text categorization（文本分类）     "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fa868891",
   "metadata": {},
   "source": [
    "> 计算机也能理解语言吗            \n",
    "> 词向量是什么"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5318dc1f",
   "metadata": {},
   "source": [
    "## 百度AI实践"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57ebc848",
   "metadata": {},
   "source": [
    "> 1.自然语言处理文本信息（实践）                        \n",
    "> 2.自然语言生成文本信息（实践）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f86ba02a",
   "metadata": {},
   "source": [
    "* 语法分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d88869d4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"text\":\"百度是一家高科技公司\",\"items\":[{\"uri\":\"\",\"formal\":\"\",\"ne\":\"ORG\",\"item\":\"百度\",\"loc_details\":[],\"basic_words\":[\"百度\"],\"byte_offset\":0,\"byte_length\":4,\"pos\":\"\"},{\"uri\":\"\",\"formal\":\"\",\"ne\":\"\",\"item\":\"是\",\"loc_details\":[],\"basic_words\":[\"是\"],\"byte_offset\":4,\"byte_length\":2,\"pos\":\"v\"},{\"uri\":\"\",\"formal\":\"\",\"ne\":\"\",\"item\":\"一家\",\"loc_details\":[],\"basic_words\":[\"一\",\"家\"],\"byte_offset\":6,\"byte_length\":4,\"pos\":\"\"},{\"uri\":\"\",\"formal\":\"\",\"ne\":\"\",\"item\":\"高科技\",\"loc_details\":[],\"basic_words\":[\"高\",\"科技\"],\"byte_offset\":10,\"byte_length\":6,\"pos\":\"n\"},{\"uri\":\"\",\"formal\":\"\",\"ne\":\"ORG\",\"item\":\"公司\",\"loc_details\":[],\"basic_words\":[\"公司\"],\"byte_offset\":16,\"byte_length\":4,\"pos\":\"\"}],\"log_id\":1650342066101996421}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "API_KEY = \"QLSO4YNbcMSc2zdOD0wIh3l7\"\n",
    "SECRET_KEY = \"Gq76lzgmI2oqzicoNzCzXZlsq2n72R0Z\"\n",
    "\n",
    "def main():\n",
    "        \n",
    "    url = \"https://aip.baidubce.com/rpc/2.0/nlp/v1/lexer?charset=UTF-8&access_token=\" + get_access_token()\n",
    "    \n",
    "    payload = json.dumps({\n",
    "        \"text\": \"百度是一家高科技公司\"\n",
    "    })\n",
    "    headers = {\n",
    "        'Content-Type': 'application/json',\n",
    "        'Accept': 'application/json'\n",
    "    }\n",
    "    \n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "    \n",
    "    print(response.text)\n",
    "    \n",
    "\n",
    "def get_access_token():\n",
    "    \"\"\"\n",
    "    使用 AK，SK 生成鉴权签名（Access Token）\n",
    "    :return: access_token，或是None(如果错误)\n",
    "    \"\"\"\n",
    "    url = \"https://aip.baidubce.com/oauth/2.0/token\"\n",
    "    params = {\"grant_type\": \"client_credentials\", \"client_id\": API_KEY, \"client_secret\": SECRET_KEY}\n",
    "    return str(requests.post(url, params=params).json().get(\"access_token\"))\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e4113a65",
   "metadata": {},
   "source": [
    "* 地址识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "4f81a0df",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"lat\":23.68823,\"detail\":\"\",\"town\":\"\",\"phonenum\":\"\",\"city_code\":\"441800\",\"province\":\"广东省\",\"person\":\"\",\"lng\":113.062468,\"province_code\":\"440000\",\"text\":\"清远\",\"county\":\"清城区\",\"city\":\"清远市\",\"county_code\":\"441802\",\"town_code\":\"\",\"log_id\":1650342839194217811}\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import json\n",
    "\n",
    "API_KEY = \"QLSO4YNbcMSc2zdOD0wIh3l7\"\n",
    "SECRET_KEY = \"Gq76lzgmI2oqzicoNzCzXZlsq2n72R0Z\"\n",
    "\n",
    "def main():\n",
    "        \n",
    "    url = \"https://aip.baidubce.com/rpc/2.0/nlp/v1/address?access_token=\" + get_access_token()\n",
    "    \n",
    "    payload = json.dumps({\n",
    "        \"text\": \"清远\"\n",
    "    })\n",
    "    headers = {\n",
    "        'Content-Type': 'application/json',\n",
    "        'Accept': 'application/json'\n",
    "    }\n",
    "    \n",
    "    response = requests.request(\"POST\", url, headers=headers, data=payload)\n",
    "    \n",
    "    print(response.text)\n",
    "    \n",
    "\n",
    "def get_access_token():\n",
    "    \"\"\"\n",
    "    使用 AK，SK 生成鉴权签名（Access Token）\n",
    "    :return: access_token，或是None(如果错误)\n",
    "    \"\"\"\n",
    "    url = \"https://aip.baidubce.com/oauth/2.0/token\"\n",
    "    params = {\"grant_type\": \"client_credentials\", \"client_id\": API_KEY, \"client_secret\": SECRET_KEY}\n",
    "    return str(requests.post(url, params=params).json().get(\"access_token\"))\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9d7e4017",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "936edb6c",
   "metadata": {},
   "source": [
    "## 对话机器人"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2a4038c4",
   "metadata": {},
   "source": [
    "> 对话机器人（实践）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "fb56ab47",
   "metadata": {},
   "outputs": [],
   "source": [
    "import requests,json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "5fd7888c",
   "metadata": {},
   "outputs": [],
   "source": [
    "url = 'http://openapi.turingapi.com/openapi/api/v2'\n",
    "data = {\n",
    "    \"reqType\":0,\n",
    "    \"perception\": {\n",
    "        \"inputText\": {\n",
    "            \"text\": \"聊点什么呢\"\n",
    "        },\n",
    "        \"inputImage\": {\n",
    "            \"url\": \"\"\n",
    "        },\n",
    "\n",
    "    },\n",
    "    \"userInfo\": {\n",
    "        \"apiKey\": \"d5d5bb66f66d4da8bedb378df7adfa70\",\n",
    "        \"userId\": \"111\"\n",
    "    }\n",
    "}\n",
    "\n",
    "r = requests.post(url,json.dumps(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c4f098e9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<Response [200]>"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "db41ce3b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'emotion': {'robotEmotion': {'a': 0, 'd': 0, 'emotionId': 0, 'p': 0},\n",
       "  'userEmotion': {'a': 0, 'd': 0, 'emotionId': 0, 'p': 0}},\n",
       " 'intent': {'appKey': 'platform.chat', 'code': 0, 'operateState': 1100},\n",
       " 'results': [{'groupType': 0,\n",
       "   'resultType': 'text',\n",
       "   'values': {'text': '聊点什么都可以，天南地北都可以聊'}}]}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "r.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8e06520d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
